package com.yimixian.app.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.yimixian.app.R;
import com.yimixian.app.activity.BaseNaviFragmentGroupActivity;
import com.yimixian.app.cart.CartManager;
import com.yimixian.app.common.Constants;
import com.yimixian.app.model.SessionDataBean;
import com.yimixian.app.rest.api.CartUpdateAPI;
import com.yimixian.app.ui.BaseHttpFragment;
import com.yimixian.app.ui.LoadingPage;
import com.yimixian.app.ui.MessageDialogView;
import com.yimixian.app.ui.PlusMinusIcon;
import com.yimixian.app.util.UiUtils;
import com.ymx.sdk.util.ViewUtils;

import org.json.JSONException;


/**
 * 
 */
public abstract class BaseFragment extends BaseHttpFragment implements OnClickListener {


	protected ViewGroup mDialogView;
	protected CartUpdateAPI mCartUpdateAPI;
	protected PlusMinusIcon mPlusMinusIcon;


	protected BaseNaviFragmentGroupActivity mActivity;

	protected LoadingPage mContentView;

	public BaseFragment() {
	}

	public BaseFragment(BaseNaviFragmentGroupActivity activity, View emptyView) {
		mActivity = activity;
		mContentView = new LoadingPage(mActivity, emptyView) {

			@Override
			public void load() {
				BaseFragment.this.load();
			}

			@Override
			public View createLoadedView() {
				return BaseFragment.this.createLoadedView();
			}

			@Override
			public void loadMain() {
				BaseFragment.this.loadMain();
			}

		};
	}

	public BaseFragment(BaseNaviFragmentGroupActivity activity) {
		this(activity, null);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		ViewUtils.removeSelfFromParent(mContentView);
		return mContentView;
	}

	// 加载一次，直接显示
	public void show() {
		if (mContentView != null) {
			mContentView.show();
		}
	}

	// 加载一次，直接显示
	public void setLoadResult(LoadingPage.LoadResult result) {
		if (mContentView != null) {
			mContentView.setLoadResult(result);
		}
	}

	// 重新加载数据
	public void showLoad() {
		if (mContentView != null) {
			mContentView.showLoad();
		}
	}

	// 重新加载布局和数据
	public void showLoad(boolean isLoadView) {
		if (mContentView != null) {
			mContentView.showLoad(isLoadView);
		}
	}

	public void showMain() {
		if (mContentView != null) {
			mContentView.showMain();
		}
	}

	protected abstract void loadMain();

	protected abstract void load();

	protected abstract View createLoadedView();



	protected void showCountLimitDialog() {
		final MessageDialogView messageDialogView = new MessageDialogView(getActivity());
		messageDialogView.bind(getString(R.string.confirm), "亲，每次只支持换购一个品，数量有限，先到先得哦！",
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						mDialogView.removeView(messageDialogView);
					}
				});
		mDialogView.addView(messageDialogView);
	}

	protected void showMoneyLimitDialog(float price) {
		final MessageDialogView messageDialogView = new MessageDialogView(getActivity());
		messageDialogView.bind(getString(R.string.confirm), "选购满 "
						+ price + "元就可以点击换购此品啦！数量有限，快快抢购吧！",
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						mDialogView.removeView(messageDialogView);
					}
				});
		mDialogView.addView(messageDialogView);
	}


	public void setDialogView(ViewGroup viewGroup) {
		mDialogView = viewGroup;
	}

	protected void fetchUpdateCartData() {
		fetchData(true, CartUpdateAPI.CART_UPDATE_API);
	}

	protected void initCartUpdateAPI() {
		mCartUpdateAPI = new CartUpdateAPI(this);
	}

	/**
	 * 处理 + / - 号网络请求
	 */
	protected void dealPlusMinusProtocol() {
			showPopupWindow(getActivity(), R.id.popupwindow_container);
			if(!CartManager.getInstance().isUserLogin()){
					try {
							if (Constants.PLUS_BUTTON_CLICKED_BROADCAST.equals(mPlusMinusIcon.mButtonState)) {
								CartManager.getInstance().saveUnLoginCartData(mPlusMinusIcon.mGoodsItem.id, true, mPlusMinusIcon);
							}else if (Constants.MINUS_BUTTON_CLICKED_BROADCAST.equals(mPlusMinusIcon.mButtonState)) {
								CartManager.getInstance().saveUnLoginCartData(mPlusMinusIcon.mGoodsItem.id, false,mPlusMinusIcon);
							}
					} catch (JSONException e) {
							e.printStackTrace();
							UiUtils.showToast("数据保存失败");
					}
					hidePopupWindow();
			}else{
					fetchUpdateCartData();
					if(mPlusMinusIcon != null && mCartUpdateAPI != null) {
							mCartUpdateAPI.mGoodId = mPlusMinusIcon.mGoodsItem.id;
							if (Constants.PLUS_BUTTON_CLICKED_BROADCAST.equals(mPlusMinusIcon.mButtonState)) {
								mCartUpdateAPI.mTrend = 1;
								mCartUpdateAPI.getCartUpdate();
							}
							else if (Constants.MINUS_BUTTON_CLICKED_BROADCAST.equals(mPlusMinusIcon.mButtonState)) {
								mCartUpdateAPI.mTrend = 0;
								mCartUpdateAPI.getCartUpdate();
							}
					}
			}
	}
}
